<template>
  <view class="shop-settle-container">
    <!-- 全选 -->
    <label class="radio" @click="changeFullState">
      <radio color="#C00000" :checked="isFullCheck" /><text>全选</text>
    </label>
    <!-- 合计 -->
    <view class="amount-box">
      合计：<text class="amount">{{ checkAmount }}</text>
    </view>
    <!-- 结算按钮 -->
    <view class="btn-settle" @click="settlement">
      结算({{checkoutCount}})
    </view>
  </view>
</template>
<script>
  import {
    mapGetters,
    mapMutations,
    mapState
  } from "vuex";
  export default {
    data() {
      return {
        seconds: 3,
        timer: null
      };
    },
    computed: {
      ...mapGetters("m_cart", ["checkoutCount", "total", "checkAmount"]),
      ...mapGetters("m_user", ["addressStr"]),
      ...mapState("m_user", ["token"]),
      ...mapState("m_cart", ["cart"]),
      isFullCheck() {
        return this.total === this.checkoutCount
      }
    },

    methods: {
      ...mapMutations("m_cart", ["updateAllState"]),
      ...mapMutations("m_user", ["updateRedirectInfo"]),
      // 更改全选状态
      changeFullState() {
        this.updateAllState(!this.isFullCheck)
      },
      // 点击结算
      settlement() {
        if (!this.checkoutCount) return uni.$showMsg("请勾选结算商品")
        if (!this.addressStr) return uni.$showMsg("请选择收货地址")
        // 跳转
        if (!this.token) return this.delayNavigate();
        // 支付
        this.payOrder()
      },

      // 提示信息
      showTips(n) {
        uni.showToast({
          title: "请登录后再结算！将在第" + n + "秒后跳转到登录页",
          icon: "none",
          mask: true,
          duration: 1500
        })
      },

      // 延时跳转
      delayNavigate() {
        this.seconds = 3
        this.showTips(this.seconds)

        this.timer = setInterval(() => {
          this.seconds--;

          if (this.seconds <= 0) {
            clearInterval(this.timer);

            uni.switchTab({
              url: "/pages/my/my",
              success: () => {
                this.updateRedirectInfo({
                  openType: "switchTab",
                  from: "/pages/cart/cart"
                })
              }
            })

            return
          }
          this.showTips(this.seconds)
        }, 1000)
      },

      // 微信支付
      async payOrder() {
        // 请求参数
        const orderInfo = {
          order_price: 0.01,
          consignee_addr: this.addressStr,
          goods: this.cart.filter(x => x.goods_state).map(x => ({
            goods_id: x.goods_id,
            goods_number: x.goods_count,
            goods_price: x.goods_price
          }))
        };

        // 创建订单
        const {
          data: res
        } = await uni.$http.post("/api/public/v1/my/orders/create", orderInfo)
        if (res.meta.status !== 200) return uni.$showMsg("创建订单失败！")
        const orderNumber = res.message.order_number;

        // 预支付
        const {
          data: res2
        } = await uni.$http.post("/api/public/v1/my/orders/req_unifiedorder", {
          order_number: orderNumber
        })
        if (res2.meta.status !== 200) return uni.$showError("预付订单失败！");
        const payInfo = res2.message.pay;

        // 支付
        const [err, succ] = await uni.requestPayment(payInfo)
        console.log(err);
        if (err) return uni.$showMsg("订单未支付！")

        // 检测支付
        const res3 = await uni.$http.post("/api/public/v1/my/orders/chkOrder", {
          order_number: orderNumber
        })
        if (res3.meta.status !== 200) return uni.$showMsg("订单未支付！")
        uni.showToast({
          title: "订单支付完成！",
          icon: "success"
        })
      }
    }
  }
</script>
<style lang="scss">
  .shop-settle-container {
    position: fixed;
    left: 0;
    bottom: 0;
    width: 100%;
    height: 50px;
    background-color: cyan;
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: #FFF;
    font-style: 14px;
    padding-left: 5px;

    .radio {
      display: flex;
      align-items: center;
    }

    .amount-box {
      .amount {
        color: #C00000;
        font-weight: 800;
      }
    }

    .btn-settle {
      background-color: #C00000;
      height: 50px;
      color: #FFF;
      line-height: 50px;
      padding: 0 10px;
      min-width: 100px;
      text-align: center;
    }
  }
</style>